package cn.tedu.egmvc1.controller;

import cn.tedu.egmvc1.mapper.UserMapper;
import cn.tedu.egmvc1.pojo.dto.AddUserDTO;
import cn.tedu.egmvc1.pojo.entity.User;
import cn.tedu.egmvc1.pojo.vo.UserlistVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

//@Controller
@RestController
@RequestMapping(value = "/v1/users/")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping(value = "insert")
//    @ResponseBody
    public String adduser(AddUserDTO addUserDTO){
        User user =new User();
        BeanUtils.copyProperties(addUserDTO,user);
        user.setCreated(new Date());
        userMapper.insertUser(user);
        return "添加成功";
    }
    @GetMapping(value = "userlist")
//    @ResponseBody
    public List<UserlistVO> userlist(){
        System.out.println(userMapper.selectUsers());
        return userMapper.selectUsers();
    }

    @GetMapping(value = "delete")
//    @ResponseBody
    public String deleteUser(int id){
        userMapper.deleteUser(id);
        return "删除成功";
    }

    @PostMapping(value = "update")
    //    @ResponseBody
    public String updateUser(User user){
        userMapper.updateUser(user);
        return "修改成功";
    }

    @GetMapping(value = "{id}")
    public User getUserById(@PathVariable int id){
        return userMapper.selectUserByid(id);
    }
}
